package com.example.travel.controller;

import com.example.travel.bean.IchBean;
import com.example.travel.service.IchService;
import com.example.travel.utils.DateFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.List;
import java.util.Map;

@Controller
public class IchController {

    @Autowired
    private IchService ichService;

    @RequestMapping("/getIchList")
    public ModelAndView getIchList(@RequestParam(required = false) String searchKeyword,
                                  @RequestParam(defaultValue = "1") int page,
                                  @RequestParam(defaultValue = "10") int pageSize) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        
        // 计算偏移量
        int offset = (page - 1) * pageSize;
        
        // 获取分页数据和总数
        List<IchBean> dataList = ichService.getIchListWithPagination(searchKeyword, offset, pageSize);
        int totalCount = ichService.getIchCount(searchKeyword);
        
        modelAndView.addObject("dataList", dataList);
        modelAndView.addObject("totalCount", totalCount);
        modelAndView.addObject("currentPage", page);
        modelAndView.addObject("pageSize", pageSize);
        modelAndView.addObject("status", "success");
        return modelAndView;
    }

    @RequestMapping(value = "/addIch", method = RequestMethod.POST)
    public ModelAndView addIch(@RequestParam Map<String, Object> params) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());

        String ichName = params.get("ichName").toString();
        String ichDesc = params.get("ichDesc").toString();
        String ichPicture = params.get("ichPicture").toString();
        String ichAuthor = params.get("ichAuthor").toString();

        ichPicture = ichPicture == null || ichPicture.isEmpty() ? "" : ichPicture;

        String ichDate = DateFormatter.getDate();
        String ichStatus = "normal";

        IchBean ichBean = new IchBean();
        ichBean.setIchName(ichName);
        ichBean.setIchDesc(ichDesc);
        ichBean.setIchPicture(ichPicture);
        ichBean.setIchAuthor(ichAuthor);
        ichBean.setIchDate(ichDate);
        ichBean.setIchStatus(ichStatus);

        boolean status = ichService.addIch(ichBean);
        List<IchBean> dataList = ichService.getAllIch();
        modelAndView.addObject("dataList", dataList);
        modelAndView.addObject("status", status ? "success" : "error");
        return modelAndView;
    }

    @RequestMapping(value = "/updateIch", method = RequestMethod.POST)
    public ModelAndView updateIch(@RequestParam Map<String, Object> params) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());

        int ichID = Integer.parseInt(params.get("ichID").toString());
        String ichName = params.get("ichName").toString();
        String ichDesc = params.get("ichDesc").toString();
        String ichPicture = params.get("ichPicture").toString();
        String ichAuthor = params.get("ichAuthor") == null ? "" : params.get("ichAuthor").toString();

        ichPicture = ichPicture == null || ichPicture.isEmpty() ? "" : ichPicture;

        // 可选更新日期与状态
        String ichDate = params.get("ichDate") == null ? null : params.get("ichDate").toString();
        String ichStatus = params.get("ichStatus") == null ? null : params.get("ichStatus").toString();

        IchBean ichBean = new IchBean();
        ichBean.setIchID(ichID);
        ichBean.setIchName(ichName);
        ichBean.setIchDesc(ichDesc);
        ichBean.setIchPicture(ichPicture);
        ichBean.setIchAuthor(ichAuthor);
        if (ichDate != null) ichBean.setIchDate(ichDate);
        if (ichStatus != null) ichBean.setIchStatus(ichStatus);

        boolean status = ichService.updateIch(ichBean);
        List<IchBean> dataList = ichService.getAllIch();
        modelAndView.addObject("dataList", dataList);
        modelAndView.addObject("status", status ? "success" : "error");
        return modelAndView;
    }

    @RequestMapping(value = "/deleteIch", method = RequestMethod.POST)
    public ModelAndView deleteIch(@RequestParam Map<String, Object> params) {
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        int ichID = Integer.parseInt(params.get("ichID").toString());
        boolean status = ichService.deleteIch(ichID);
        List<IchBean> dataList = ichService.getAllIch();
        modelAndView.addObject("dataList", dataList);
        modelAndView.addObject("status", status ? "success" : "error");
        return modelAndView;
    }
}
